const router = require('koa-router')()
const { login } = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')

router.prefix('/api/user')

router.post('/login', function (ctx, next) {
    const { username, password } = ctx.request.body;
    const result = login(username, password)
    return result.then(data => {
        if (data.username) {
            // 设置session
            ctx.session.username = data.username
            ctx.session.realname = data.realname
            ctx.body = new SuccessModel({ token: 'sdfuih32i4jh2j2h34i2h3i4yh' })

            return
        } else {
            ctx.body = new ErrorModel('账号或密码错误')
        }
    })
});

module.exports = router